package com.demo.demo.service;

import com.demo.demo.dao.OrderDao;
import com.demo.demo.domain.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class OrderService {

    @Autowired
    private OrderDao orderDao;

    public long add(Order order) {
        order.setCreatedate(new Date());
        order = orderDao.saveAndFlush(order);
        if (order.getOnlyc() > 0) {
            return order.getOnlyc();
        } else {
            return -1;
        }
    }

    public List<Order> findByTime(Date beginTime, Date endTime) {
        return orderDao.findByCreatedateBetween(beginTime, endTime);
    }

    public List<Order> findByTimeAndUserId(Date beginTime, Date endTime, long userid) {
        return orderDao.findByCreatedateBetweenAndUserid(beginTime, endTime, userid);
    }

    public void delete(long Id) {
        orderDao.deleteById(Id);
    }
}
